<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="${comment}" prop="openId">
        <el-input
          v-model="queryParams.openId"
          placeholder="请输入${comment}"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="微信昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入微信昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="微信头像" prop="imgUrl">
        <el-input
          v-model="queryParams.imgUrl"
          placeholder="请输入微信头像"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="手机号" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="是否加入战队" prop="isBattalion">
        <el-select
          filterable
          v-model="queryParams.isBattalion"
          style="width: 240px"
        >
          <el-option
            v-for="dict in isBattalionOptions"
            :key="dict.val"
            :label="dict.lable"
            :value="dict.val"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属战队" prop="cityCode">
        <el-select
          filterable
          v-model="queryParams.cityCode"
          style="width: 240px"
        >
          <el-option
            v-for="dict in cityCodeOptions"
            :key="dict.id"
            :label="dict.cityName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="邀请码" prop="inviteCode">
        <el-input
          v-model="queryParams.inviteCode"
          placeholder="请输入邀请码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />isStateOptions
      </el-form-item> -->
      <el-form-item label="用户状态" prop="isState">
        <el-select
          filterable
          v-model="queryParams.isState"
          style="width: 240px"
        >
          <el-option
            v-for="dict in isStateOptions"
            :key="dict.val"
            :label="dict.lable"
            :value="dict.val"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="是否获得城市勋章" prop="isMedal">
        <el-select
          filterable
          v-model="queryParams.isMedal"
          style="width: 240px"
        >
          <el-option
            v-for="dict in isMedalOptions"
            :key="dict.val"
            :label="dict.lable"
            :value="dict.val"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="当前有效的微信步数" prop="wechatFool">
        <el-input
          v-model="queryParams.wechatFool"
          placeholder="请输入当前有效的微信步数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="炸弹数量" prop="bomb">
        <el-input
          v-model="queryParams.bomb"
          placeholder="请输入炸弹数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="护盾数量" prop="shield">
        <el-input
          v-model="queryParams.shield"
          placeholder="请输入护盾数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="积分数值" prop="integral">
        <el-input
          v-model="queryParams.integral"
          placeholder="请输入积分数值"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="当前解锁城市个数" prop="nowCityCount">
        <el-input
          v-model="queryParams.nowCityCount"
          placeholder="请输入当前解锁城市个数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="累计步数" prop="countFool">
        <el-input
          v-model="queryParams.countFool"
          placeholder="请输入累计步数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:user:add']"
        >新增</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:user:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-download"
          size="mini"
          @click="handleExportC"
          v-hasPermi="['admin:user:export']"
        >导出全国排名城市</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-download"
          size="mini"
          @click="countrySort"
          v-hasPermi="['admin:user:export']"
        >查看全国排名城市</el-button>
      </el-col>


      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-download"
          size="mini"
          @click="openB = !openB"
          v-hasPermi="['admin:user_city_join:exportCountCityUserNum']"
        >导出战队新增人数</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-download"
          size="mini"
          @click="sear2"
          v-hasPermi="['admin:user_city_join:exportCountCityUserNum']"
        >查询战队新增人数</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="110" align="center" />
        <el-table-column type="expand">
      <template slot-scope="props">
        <el-table
    ref="singleTable"
    :data="props.row.cityCodeList"
    highlight-current-row
    style="width: 100%">
    <el-table-column
      type="index"
      width="50">
    </el-table-column>

     <el-table-column
      property="cityName"
      label="城市名称"
      width="120">
    </el-table-column>
    <el-table-column
      property="cityImg"
      label="城市图片"
      width="120">

       <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.cityImg"
              :preview-src-list="[scope.row.cityImg]">
            </el-image>
          </template>
    </el-table-column>
    <el-table-column
      property="sortCode"
      label="编码"
      width="120">
    </el-table-column>

  </el-table>
      </template>
    </el-table-column>
      <el-table-column label="微信昵称" align="center" prop="nickName" />
      <el-table-column label="微信头像" align="center" prop="imgUrl" >
         <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.imgUrl"
              :preview-src-list="[scope.row.imgUrl]">
            </el-image>
          </template>
      </el-table-column>
      <!-- <el-table-column label="手机号" align="center" prop="phoneNumber" /> -->
      <el-table-column label="是否加入战队" align="center" prop="isBattalion" >
        <template slot-scope="scope">
          {{scope.row.isBattalion == 1 ? '已加入' :'未加入'}}
        </template>
      </el-table-column>
      <el-table-column label="当前战队" align="center" prop="cityCode" :formatter="cityCodeFormatter"  />
      <el-table-column label="邀请码" align="center" prop="inviteCode" />
      <el-table-column label="用户状态" align="center" prop="isState" >
          <template slot-scope="scope">
          {{scope.row.isState == 1 ? '可用' :'禁用'}}
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否获得城市勋章 0未获取 1获取" align="center" prop="isMedal" /> -->
      <el-table-column label="当前有效的微信步数" align="center" prop="wechatFool" />
      <el-table-column label="炸弹数量" align="center" prop="bomb" />
      <el-table-column label="护盾数量" align="center" prop="shield" />
      <el-table-column label="积分数值" align="center" prop="integral" />
      <el-table-column label="当前解锁城市个数" align="center" prop="nowCityCount" />
      <el-table-column label="累计步数" align="center" prop="countFool" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="查看全国排名列表" :visible.sync="openC" width="1300px" append-to-body>
      <el-table :data="countrySortList" >

        <el-table-column label="微信昵称" align="center" prop="nickName" />
        <el-table-column label="微信头像" align="center" prop="imgUrl" >
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              :src="scope.row.imgUrl"
              :preview-src-list="[scope.row.imgUrl]">
            </el-image>
          </template>
        </el-table-column>

        <el-table-column label="当前战队" align="center" prop="cityName"   />

        <!-- <el-table-column label="是否获得城市勋章 0未获取 1获取" align="center" prop="isMedal" /> -->
        <el-table-column label="累计步数" align="center" prop="countFool" />
        <el-table-column label="解锁点位" align="center" prop="nowCountCity" />
        <el-table-column label="解锁共耗多久" align="center" prop="countDayTime" />
        <el-table-column label="收货地址简称" align="center" prop="addrName" />
        <el-table-column label="收货地址详情" align="center" prop="addrDetail" />
        <el-table-column label="收货人姓名" align="center" prop="proName" />
        <el-table-column label="收货人电话" align="center" prop="phone" />

      </el-table>

      <pagination
        v-show="totalC>0"
        :total="totalC"
        :page.sync="queryParamsC.pageNum"
        :limit.sync="queryParamsC.pageSize"
        @pagination="getListC"
      />
    </el-dialog>

    <!-- 添加或修改客户端用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <!-- <el-form-item label="${comment}" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入${comment}" />
        </el-form-item> -->
        <el-form-item label="微信昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入微信昵称" />
        </el-form-item>
        <el-form-item label="微信头像" prop="imgUrl">
          <el-upload class="avatar-uploader" :action="fileUrl" :show-file-list="false" :on-success="successFile" :before-upload="beforeAvatarUpload">
            <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar" style="width:200px;height:200px;">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="是否加入战队" prop="isBattalion">
           <el-select
          filterable
          v-model="form.isBattalion"
          style="width: 240px"
        >
          <el-option
            v-for="dict in isBattalionOptions"
            :key="dict.val"
            :label="dict.lable"
            :value="dict.val"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="所属战队" prop="cityCode">
          <el-select
          filterable
          v-model="form.cityCode"
          style="width: 240px"
        >
          <el-option
            v-for="dict in cityCodeOptions"
            :key="dict.id"
            :label="dict.cityName"
            :value="dict.id"
          />
        </el-select>
        </el-form-item>
        <el-form-item label="邀请码" prop="inviteCode">
          <el-input disabled v-model="form.inviteCode" placeholder="请输入邀请码" />
        </el-form-item>
        <el-form-item label="状态" prop="isState">
           <el-select
          filterable
          v-model="form.isState"
          style="width: 240px"
        >
          <el-option
            v-for="dict in isStateOptions"
            :key="dict.val"
            :label="dict.lable"
            :value="dict.val"
          />
        </el-select>
        </el-form-item>
        <!-- <el-form-item label="是否获得城市勋章 0未获取 1获取" prop="isMedal">
          <el-input v-model="form.isMedal" placeholder="请输入是否获得城市勋章 0未获取 1获取" />
        </el-form-item> -->
        <el-form-item label="当前有效的微信步数" prop="wechatFool">
          <el-input v-model="form.wechatFool" placeholder="请输入当前有效的微信步数" />
        </el-form-item>
        <el-form-item label="炸弹数量" prop="bomb">
            <el-input-number v-model="form.bomb"  :min="0" :max="1000000" label="炸弹数量"></el-input-number>
        </el-form-item>
        <el-form-item label="护盾数量" prop="shield">
        <el-input-number v-model="form.shield"  :min="0" :max="1000000" label="炸弹数量"></el-input-number>
        </el-form-item>
        <el-form-item label="积分数值" prop="integral">
                 <el-input-number v-model="form.integral"  :min="0" :max="1000000" label="炸弹数量"></el-input-number>

        </el-form-item>
        <el-form-item label="当前解锁城市个数" prop="nowCityCount">
          <el-input disabled v-model="form.nowCityCount" placeholder="请输入当前解锁城市个数" />
        </el-form-item>
        <el-form-item label="累计步数" prop="countFool">
          <el-input disabled v-model="form.countFool" placeholder="请输入累计步数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>








    <el-dialog  :visible.sync="openB" width="800px" append-to-body>
      <el-form  label-width="110px">
        <el-form-item label="时间区间查询" prop="dateRanges">
          <el-date-picker
            unlink-panels
            v-model="dateRanges"
            type="daterange"
            align="right"
            format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
           >
          </el-date-picker>
        </el-form-item>


        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-download"
            size="mini"
            @click="handleExportB"
            v-hasPermi="['admin:user_city_join:exportCountCityUserNum']"
          >导出战队新增人数</el-button>
        </el-col>



      </el-form>

    </el-dialog>




    <el-dialog  :visible.sync="openD" width="800px" append-to-body>
      <el-form  label-width="110px">
        <el-form-item label="时间区间查询" prop="dateRanges">
          <el-date-picker
            unlink-panels
            v-model="dateRanges"
            type="daterange"
            align="right"
            format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
          <el-form-item>
            <el-button type="cyan" icon="el-icon-search" size="mini" @click="searchAdd(true)">搜索</el-button>
          </el-form-item>
        </el-form-item>



        <el-table :data="countCityUserVoList" >



          <el-table-column label="战队" align="center" prop="cityName"   />

          <!-- <el-table-column label="是否获得城市勋章 0未获取 1获取" align="center" prop="isMedal" /> -->
          <el-table-column label="时间区间" align="center" prop="createTime" />
          <el-table-column label="总数" align="center" prop="userCount" />


        </el-table>



      </el-form>

    </el-dialog>

  </div>
</template>

<script>
import {listUser, getUser, delUser, addUser, updateUser, exportUser, exportUserC,exportUserB, countrySortList,countCityUserNumByTime} from "@/api/admin/user";
import { listCity_code } from "@/api/admin/city_code";
import { fileUrl } from "@/constant/constant"

export default {
  name: "User",
  data() {
    return {
      fileUrl:fileUrl,
      countCityUserVoList:[],
      dateRanges:{},
      openC:false,
      openD:false,
      countrySortList:[],
      cityCodeOptions:[],
      openB:false,
      isBattalionOptions:[
        {
          "lable":'已加入',
          "val":1
        },
                {
          "lable":'未加入',
          "val":0
        }
      ],
      isStateOptions:[
         {
          "lable":'可用',
          "val":1
        },
                {
          "lable":'禁用',
          "val":0
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalC: 0,

      // 客户端用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParamsC:{
        pageNum: 1,
        pageSize: 10,
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        openId: null,

        nickName: null,

        imgUrl: null,

        phoneNumber: null,

        isBattalion: null,

        cityCode: null,

        inviteCode: null,

        isState: null,

        isMedal: null,

        wechatFool: null,

        bomb: null,

        shield: null,

        integral: null,

        nowCityCount: null,

        countFool: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        inviteCode: [
          { required: true, message: "邀请码不能为空", trigger: "blur" }
        ],

      }
    };
  },
  created() {
    listCity_code({pageNum:1,pageSize:1000000}).then(res =>{
      this.cityCodeOptions = res.rows;
    })
    this.getList();
  },
  methods: {
        /** 售后图片上传路径  */
    successFile(res, file) {
      this.form.imgUrl = URL.createObjectURL(file.raw);
      this.form.imgUrl = res.data;
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg';
      const isJPG = true;
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
        cityCodeFormatter (row){
      var temp;
      this.cityCodeOptions.forEach(item =>{
        if (row.cityCode == item.id){
          temp =  item.cityName
        }
      });
      return temp;
    },

    /** 查询客户端用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListC() {
      countrySortList(this.queryParamsC).then(response => {
        this.countrySortList = response.data.list;
        this.totalC = response.data.total;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        openId: null,

        nickName: null,

        imgUrl: null,

        phoneNumber: null,

        isBattalion: null,

        cityCode: null,

        inviteCode: null,

        createTime: null,

        updateTime: null,

        createBy: null,

        updateBy: null,

        isState: null,

        isMedal: null,

        wechatFool: null,

        bomb: null,

        shield: null,

        integral: null,

        nowCityCount: null,

        countFool: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户端用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户端用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除客户端用户编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUser(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有客户端用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    },
    countrySort(){
      this.openC = true;
      this.getListC();
    },
    /** 导出按钮操作 */
    handleExportC() {
      this.$confirm('是否确认导出所有数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return exportUserC();
      }).then(response => {
        this.download(response.msg);
      })
    },

    sear2(){
      this.openD = !this.openD;
      this.searchAdd();
    },
    searchAdd(){
      var date = {}
      if(this.dateRanges.length >=2){
        date = {date:this.dateRanges[0],endTime:this.dateRanges[1]}
      }
      countCityUserNumByTime(date).then(res =>{
        this.countCityUserVoList = res.data;
      })

    },
    /** 导出按钮操作 */
    handleExportB() {
      this.$confirm('是否确认导出所有数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(()=> {
        console.log(this.dateRanges)
        var date = {}
        if(this.dateRanges.length >=2){
          date = {date:this.dateRanges[0],endTime:this.dateRanges[1]}
        }

        return exportUserB(date);
      }).then(response => {
        this.download(response.msg);
      })
    }
  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 122px;
  height: 122px;
  line-height: 122px;
  text-align: center;
}

.avatar {
  width: 122px;
  height: 122px;
  display: block;
}

  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>
